package com.huanglj.himalaya.adapters;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.huanglj.himalaya.R;
import com.ximalaya.ting.android.opensdk.model.track.Track;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

public class DetailAdapter extends RecyclerView.Adapter<DetailAdapter.InnerHolder> {

    List<Track> mTracks = new ArrayList<>();
    //格式化时间
    private SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
    private SimpleDateFormat mDurationFormat = new SimpleDateFormat("mm:ss");
    private OnDetailOnclickListener mListener;
    private OnLongClickListener mLongListener;

    @NonNull
    @Override
    public InnerHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.detail_list_item, parent, false);
        return new InnerHolder(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull InnerHolder holder, final int position) {
        final View itemView = holder.itemView;
        itemView.setTag(position);
        holder.setData(mTracks.get(position));
        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mListener.onItemClick(mTracks,position);
            }
        });
        itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                mLongListener.onLongItemClick(mTracks.get(position));
                return true;
            }
        });
    }

    @Override
    public int getItemCount() {
        if (mTracks != null) {
            return mTracks.size();
        }
        return 0;
    }

    public void setData(List<Track> tracks) {
        mTracks.clear();
        mTracks.addAll(tracks);
        notifyDataSetChanged();
    }

    public class InnerHolder extends RecyclerView.ViewHolder {
        public InnerHolder(@NonNull View itemView) {
            super(itemView);
        }

        public void setData(Track track) {
            int tag = (int) itemView.getTag();
            TextView orderText = itemView.findViewById(R.id.order_text);
            TextView itemTitle = itemView.findViewById(R.id.detail_item_title);
            TextView detailTime = itemView.findViewById(R.id.detail_time);
            TextView playCount = itemView.findViewById(R.id.item_play_count);
            TextView playTime = itemView.findViewById(R.id.item_play_time);
            orderText.setText(tag+1 + "");
            itemTitle.setText(track.getTrackTitle());
            String updateTime = mSimpleDateFormat.format(track.getUpdatedAt());
            detailTime.setText(updateTime);
            playCount.setText(track.getPlayCount()+"");
            int durationMil = track.getDuration() * 1000;
            String durationTime = mDurationFormat.format(durationMil);
            playTime.setText(durationTime);
        }
    }
    public void setOnDetailOnclickListener(OnDetailOnclickListener listener){
        mListener = listener;
    }
    public interface OnDetailOnclickListener{
        void onItemClick(List<Track> trackList, int position);
    }

    public void setOnLongClickListener(OnLongClickListener listener){
        mLongListener = listener;
    }

    public interface OnLongClickListener{
        void onLongItemClick(Track track);
    }
}
